import java.security.MessageDigest;

public class MD5 {
    public static String MD5(String key) throws Exception {
        byte[] cipher = key.getBytes();
        MessageDigest clear = MessageDigest.getInstance("MD5");
        clear.update(cipher);
        byte s[] = clear.digest();
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < s.length; i++) {
            int a = ((int) s[i]) & 0xff;
            if (a < 16) {
                str.append("0");
            }
            str.append(Integer.toHexString(a));
        }
        return str.toString();
    }

    public static void main(String args[]) throws Exception {
        String str1 = new String("dingyi");
        System.out.println("原始：" + str1);
        System.out.println("MD5后：" + MD5(str1));
        String str2 = new String("kongyue");
        System.out.println("原始：" + str2);
        System.out.println("MD5后：" + MD5(str2));
        String str3 = new String("abc");
        System.out.println("原始：" + str3);
        System.out.println("MD5后：" + MD5(str3));
    }
}
